﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
using System.Web.Services;
using DAO;
using System.Drawing;

namespace TuyenDungViecLam.NhaTuyenDung
{
    public partial class NhaTuyenDungDangKy : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {


                DrlTinhThanh.DataSource = ChiTietCongViec_BUS.DanhSachNoiLamViec();
                DrlTinhThanh.DataTextField = "Ten";
                DrlTinhThanh.DataValueField = "MaTinhThanh";
                DrlTinhThanh.DataBind();
            }
        }

        protected void BtDangKy_Click(object sender, EventArgs e)
        {
            try
            {
                NhaTuyenDung_DTO nhaTuyenDung = new NhaTuyenDung_DTO();

                nhaTuyenDung.UserName = txtUsername.Text;
                nhaTuyenDung.PassWord = txtPassword.Text;
                nhaTuyenDung.Date = DateTime.Now.Date;
                nhaTuyenDung.Ten = txtTen.Text;
                nhaTuyenDung.GioiThieu = TxtGioiThieu.Text;
                nhaTuyenDung.QuyMo = TxtQuymo.Text;
                nhaTuyenDung.NguoiLienHe = TxtNguoiLienHe.Text;
                nhaTuyenDung.Fax = Txtfax.Text;
                nhaTuyenDung.SoTaiKhoan = TxtSoTaiKhoan.Text;
                nhaTuyenDung.Email = txtEmail.Text;
                nhaTuyenDung.DienThoai = txtdienthoai.Text;
                nhaTuyenDung.Didong = txtdidong.Text;
                nhaTuyenDung.NgayDangKy = DateTime.Now.Date;
                nhaTuyenDung.DiaChi = txtDiachi.Text;
                nhaTuyenDung.MaTinhThanh = int.Parse(DrlTinhThanh.SelectedItem.Value.ToString());


                if (IsUserAvailable(txtUsername.Text))
                {
                    if (txtCaptcha.Text.Equals(Session["captcha"].ToString(), StringComparison.OrdinalIgnoreCase))
                    {

                        bool kq = NhaTuyenDung_BUS.Register(nhaTuyenDung);

                        if (kq == true)
                        {
                            divCapnhat.Visible = false;
                            divThongbao.Visible = true;
                            lbThongbao.Text = "đăng kí thành công";
                            lbThongbao.ForeColor = Color.Blue;
                        }
                        else
                        {
                            divThongbao.Visible = true;
                            lbThongbao.Text = "đăng kí thất bại";
                            lbThongbao.ForeColor = Color.Red;
                        }
                        lblMessage.Text = "";
                    }
                    else
                        lblMessage.Text = "Vui lòng nhập đúng chuỗi xác nhận";
                }
                else
                {
                    lblMessage.Text = "Username này đã tồn tại";
                }
            }
            catch (Exception)
            {
                lblMessage.Text = "xác nhận capcha có lỗi";
            }
        }

        [WebMethod]
        public static bool IsUserAvailable(string username)
        {
            MyMembershipProvider myMembership = new MyMembershipProvider();

            bool kq = myMembership.GetUser(username);

            if (kq == true)
            {
                return false;
            }
            else
            {
                return true;
            }


        }
    }
}
